﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using StudentModule.Models;
using StudentModule.Services;
using ServiceAgent;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using StudentModule.Views;
using ServiceAgent.StudentService;
using System.Collections.Generic;
using ServiceAgent.CatalogueService;
using StudentModule.Commands;
using Infrastructure.Account;
//using StudentModule.Events;

namespace StudentModule.ViewModels
{
    /// <summary>
    /// ViewModel for BasicSearchSinhVienView.
    /// </summary>
    public class BasicSearchSinhVienViewModel : NotificationObject, INavigationAware, IRegionMemberLifetime
    {
        #region Fields
        private IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private IRegionManager regionManager;

        string query;

        ObservableCollection<SinhVienBasicDTO> list = new ObservableCollection<SinhVienBasicDTO>();
        SinhVienBasicDTO currentItem;
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public BasicSearchSinhVienViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;

            this.Initialize();
        }
        #endregion

        #region Administrative Properties
        public string Query
        {
            get { return query; }
            set
            {
                query = value;
                RaisePropertyChanged(() => this.Query);
                //this.SearchCommand.RaiseCanExecuteChanged();
            }
        }

        public ObservableCollection<SinhVienBasicDTO> List
        {
            get { return list; }
            set
            {
                list = value;
                RaisePropertyChanged(() => this.List);
            }
        }

        public SinhVienBasicDTO CurrentItem
        {
            get { return currentItem; }
            set
            {
                currentItem = value;
                RaisePropertyChanged(() => this.List);
            }
        }

        public IRegionManager RegionManager
        {
            get { return regionManager; }
            set { regionManager = value; }
        }

        public IEventAggregator EventAggregator
        {
            get { return eventAggregator; }
            set { eventAggregator = value; }
        }
        #endregion

        #region Command Properties
        public ICommand EditCommand { get; private set; }
        public DelegateCommand SearchCommand { get; private set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {

            this.EditCommand = container.Resolve<ShowNhapThongTinViewCommand>(
                new ParameterOverrides 
                {
                    {"viewModel", this}
                }.OnType<ShowNhapThongTinViewCommand>()
            );
            this.SearchCommand = new DelegateCommand(this.OnRaiseSearch);

        }

        private void OnRaiseSearch()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {        
                        List = proxy.SearchSinhVien(this.Query);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private bool CanRaiseSearch()
        {
            return !string.IsNullOrEmpty(Query);
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            //string id = navigationContext.Parameters["ID"];
            //if (!string.IsNullOrEmpty(id))
            //{
            //    DataItem item = _dataService.GetModel().FirstOrDefault(dataItem => dataItem.Id == id);
            //}
        }
        #endregion

        #region IRegionMemberLifetime Members
        public bool KeepAlive
        {
            get { return true; }
        }
        #endregion
    }
}
